// pages/holiday/holiday.js
const app = getApp()
const api = require("../../config/api.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //组件数据
    value: '',
    active: 1,
    minHour: 10,
    maxHour: 20,
    minDate: new Date().getTime(),
    maxDate: new Date(2021, 10, 1).getTime(),
    currentDate: new Date().getTime(),
    show: false,
    show1: false,
    activeName: '1',

    //交互数据
    username:'',
    reason:'',
    start :'',
    end:'',
    holiday:{},
    list:[],
    role: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.stopPullDownRefresh()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // this.setData({
    //   role:app.globalData.role
    // })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  onChange(event) {
    this.setData({
      activeName: event.detail,
    })},
  getValue(e) {
    console.log(e.detail)
  },
  onConfirmStart(event) {
    console.log(event.detail)
    var time = new Date(event.detail)
    console.log(time)
    var time1 = format(time)
    console.log("start")
    console.log(time1)
    this.setData({
      show: false,
      start:time1
    });
  },
  onConfirmEnd(event) {
    console.log(event.detail)
    var time = new Date(event.detail)
    console.log(time)
    var time1 = format(time)
    console.log("end")
    console.log(time1)
    this.setData({
      show1: false,
      end:time1
    });
  },
  showPopup() {
    this.setData({
      show: true
    });
  },
  showPopup1() {
    this.setData({
      show1: true
    });
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onLoad()
  },
  onClose() {
    this.setData({
      show: false,
      show1:false
    });
  },
  submit(){
    if (app.globalData.userId == null) {
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      })
      return;
    }
    var that=this
    wx.showModal({
      title: '提示',
      content: '您确定提交吗',
      success (res) {
        if (res.confirm) {
          console.log('用户点击确定')
          var holiday=that.data.holiday
          holiday.username=that.data.username
          holiday.reason=that.data.reason
          holiday.start=that.data.start
          holiday.end=that.data.end
          holiday.userId=app.globalData.userId
          console.log(holiday)
          console.log(JSON.stringify(holiday))
          wx.request({
            url: api.holiday,
            data: JSON.stringify(holiday),
            method: "POST",
            success: function (res) {
              wx.showToast({
                title: '提交成功',
              })
              that.onLoad()
              that.setData({
                username:'',
                reason:'',
                start :'',
                end:'',
              })
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })

  },
  userGetHoliday(){
    if (app.globalData.userId == null) {
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      })
      return;
    }
    var that=this
    wx.request({
      url: api.holiday+'?userId='+app.globalData.userId,
      method:"GET",
      success(res){
      console.log(res)
      that.setData({
        list:res.data.data
      })
      console.log(that.data.list)
      }
    })
  },

})

function format(date) {
  var y = date.getFullYear();
  var m = date.getMonth() + 1;
  m = m < 10 ? ('0' + m) : m;
  var d = date.getDate();
  d = d < 10 ? ('0' + d) : d;
  var h = date.getHours();
  h = h < 10 ? ('0' + h) : h;
  var minute = date.getMinutes();
  minute = minute < 10 ? ('0' + minute) : minute;
  var second = date.getSeconds();
  second = second < 10 ? ('0' + second) : second;
  return y + '-' + m + '-' + d + ' ' + h + ':' + minute;
}